﻿using System.Web.Mvc;
using AutoMapper;
using Sanelib.CodeMaker;
using Sanelib.Common.SystemSettings;
using WebApp.Services;

namespace WebApp.Controllers
{
    public class GeneratorController : SmartController
    {
        public GeneratorController(IUserSession userSession, IMappingEngine mappingEngine)
            : base(userSession, mappingEngine)
        { }

        public ActionResult Index()
        {
            var dbSchema = Sanelib.Common.SerializerHelper.Xml2Obj<DatabaseSchema>.Load();
            if (!string.IsNullOrEmpty(dbSchema.DatabaseName)) return View(dbSchema);
            var dbConfig = Sanelib.Common.SerializerHelper.Xml2Obj<DatabaseConfig>.Load();
            dbSchema.DatabaseName = dbConfig.DatabaseName;
            dbSchema.Description = "Generated from code";
            Sanelib.Common.SerializerHelper.Xml2Obj<DatabaseSchema>.Save(dbSchema);
            return View(dbSchema);
        }
    }
}
